package com.pu.controller.user;

import com.pu.constant.StatusConstant;
import com.pu.entity.Setmeal;
import com.pu.result.Result;
import com.pu.service.SetmealService;
import com.pu.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Yusc
 * @Date 2023/10/31 18:30
 * @Version
 * @Description C端-套餐浏览
 * 优化：用spring-cache框架进行缓存机制
 **/
@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
@Api(tags = "C端-套餐浏览接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * @param categoryId
     * @auther: Yusc
     * @date 2023/10/31 18:31
     * @Description 条件查询 - 根据分类id和起售状态查询
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    // 在方法执行前先查询缓存中是否有数据，如果有数据，则直接返回缓存数据；如果没有缓存数据，调用方法并将方法返回值放到缓存中
    @Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key:setmealCache::categoryId
    public Result<List<Setmeal>> list(Long categoryId) {
        log.info("根据分类id查询套餐:{}", categoryId);
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, StatusConstant.ENABLE)
                .list();
        return Result.success(list);
    }

    /**
     * @param id 套餐id
     * @auther: Yusc
     * @date 2023/10/31 19:38
     * @Description 根据套餐id查询包含的菜品列表 -- 两表操作(套餐_菜品表,菜品表)
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据套餐id查询包含的菜品列表")
    public Result<List<DishItemVO>> dishList(@PathVariable("id") Long id) {
        log.info("根据套餐id查询包含的菜品列表:{}", id);
        List<DishItemVO> list = setmealService.getDishItemById(id);
        return Result.success(list);
    }
}
